#pragma once
#include <iostream>
#include <string>
#include <jsoncpp/json/json.h>
#include <memory>
#include <sstream>
// #include "Calculator.hpp"

const std::string sep = "\r\n";

// 添加长度报头
void Encode(std::string &cmdstr)
{
    std::string s = std::to_string(cmdstr.size()) + sep + cmdstr + sep;
    cmdstr = s;
}
// 解决读取不完善的问题
// 解析发送过来的报头
// length\r\n
// length\r\n{json}
// length\r\n{json}\r
// length\r\n{json}\r\n -> 解析这个
// length\r\n{json}\r\nlength\r\n{json}\r\n
bool Decode(std::string &package, std::string *out_str)
{
    // 如果没有分割符，说明不符合要求
    if (package.find(sep) == std::string::npos)
        return false;
    auto pos = package.find(sep);
    int content_length = std::stoi(package.substr(0, pos));
    // 报文的总长度
    int full_length = content_length + 2 * sep.size() + pos;
    if (package.size() < full_length)
        return false;
    // 获取有效报文
    *out_str = package.substr(pos + sep.size(), content_length);
    // 删除当前解析过的一条完成报文
    package.erase(0, full_length);
    return true;
}

class Request
{
public:
    Request() :_x(0),_y(0),_oper(0) {}
    Request(int x, int y, char oper )
        : _x(x),
          _y(y),
          _oper(oper)
    {
    }
    // 序列化和反序列化不会失败
    bool serialization(std::string *out_str)
    {
        Json::Value root;
        root["x"] = _x;
        root["oper"] = _oper;
        root["y"] = _y;
        Json::StreamWriterBuilder wbuilder; // StreamWriterBuilder的工厂
        std::unique_ptr<Json::StreamWriter> writer(wbuilder.newStreamWriter());
        std::stringstream ss;
        writer->write(root, &ss);
        *out_str = ss.str();
        return true;
    }
    bool deserialization(std::string &in_str)
    {
        // 解析 JSON 字符串
        Json::Reader reader;
        Json::Value root;
        // 从字符串获取JSON数据
        bool parsingSuccessful = reader.parse(in_str, root);
        if (!parsingSuccessful)
        {
            // 解析失败， 输出错误信息
            std::cout << "Failed to parse JSON: " << reader.getFormattedErrorMessages() << std::endl;
            return false;
        }
        _x = root["x"].asInt();
        _y = root["y"].asInt();
        _oper = root["oper"].asInt();
        return true;
    }
    char Oper() const { return _oper; }
    int X() const { return _x; }
    int Y() const { return _y; }
    ~Request() {}

private:
    int _x;
    int _y;
    char _oper;
};


class Response
{
public: 
    Response() :_result(0),_code(0) {}
    Response(int result , int code)
        : _result(result),
          _code(code)
    {
    }
    ~Response() {}

    // 序列化和反序列化不会失败
    bool serialization(std::string *out_str)
    {
        Json::Value root;
        root["result"] = _result;
        root["code"] = _code;
        Json::StreamWriterBuilder wbuilder; // StreamWriterBuilder的工厂
        std::unique_ptr<Json::StreamWriter> writer(wbuilder.newStreamWriter());
        std::stringstream ss;
        writer->write(root, &ss);
        *out_str = ss.str();
        return true;
    }
    bool deserialization(std::string &in_str)
    {
        // 解析 JSON 字符串
        Json::Reader reader;
        Json::Value root;
        // 从字符串获取JSON数据
        bool parsingSuccessful = reader.parse(in_str, root);
        if (!parsingSuccessful)
        {
            // 解析失败， 输出错误信息
            std::cout << "Failed to parse JSON: " << reader.getFormattedErrorMessages() << std::endl;
            return false;
        }
        _result = root["result"].asInt();
        _code = root["code"].asInt();
        return true;
    }
    void Print()
    {
        std::cout << "############################" << std::endl;
        std::cout << "result: " << _result << std::endl;
        std::cout << "code: " << _code << std::endl;
        std::cout << "############################" << std::endl;

    }
    void SetResult(int result) { _result = result; }
    void SetCode(int code) { _code = code; }

private:
    int _result;
    int _code;
};

